home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-11-10 | 13.3 KB | 351 lines |
- /**
- /*
- Rob Mayfield. IMSI @1997
- The text window uses a small subset of HTML to display multiple color
- in the text. This is done in the displayText class.
- Eventually Multi font, size, style and color text box
- **/
- import java.awt.*;
-
- public class textDisplay extends Canvas
- {
- String Text;
- int width, height, left, top; // area of text box
- java.awt.Color foreGround, backGround;
- java.util.Vector dText = new java.util.Vector();
- java.util.Vector dColor = new java.util.Vector();
- int curPos = 0;
- int viewLeftPos = 0;
- int topLine = 0; // first line on screen
- int prevTopLine = -1; // previous top line on screen. Used in checking if repaint needed.
- int aveCharWidth = 0;
- int textLengthMax = 0, textCharLenghtMax = 0;
- int totalLines = 0;
- int lineHeight = 0;
- int lines = 0;
- boolean first = true;
- ViewSourceDialog parent;
- //Color prevTopColor = Color.black;
- Font topFont;
-
- int wow=0;
-
- textDisplay ()
- { super();
- foreGround = Color.black;
- backGround = Color.white;
- this.parent = (ViewSourceDialog) getParent();
- }
-
- public void setText(String Str)
- {
- int jj=0;
- Text = Str;
- String S;
- Color prevColor = foreGround; // set up first color for line 1
- int cnt = 0;
- while ((S = getNextString()) != null)
- {
- String trimStr = rTrim(S);
- dText.addElement(trimStr); // add to Vector
-
- boolean tag = false;
- int cntChar = 0;
- for (int ii = 0; ii< trimStr.length(); ii++)
- { if (trimStr.charAt(ii) == '<') tag = true;
- if (trimStr.charAt(ii) == '>') tag = false;
- if (!tag) cntChar++;
- }
- if (cntChar > textCharLenghtMax)
- textCharLenghtMax = cntChar;
-
- Color c;
- if (cnt == 0)
- { // find start color for first line
- String upStr = trimStr.toUpperCase();
- int fPos, cPos;
- if ((fPos=upStr.indexOf("<FONT")) > -1 && (cPos=upStr.indexOf("COLOR=")) > -1)
- { if (fPos != 0) // font not set in first chars
- prevColor = foreGround;
- else
- prevColor = getFirstColor (trimStr);
- }
- else
- prevColor = foreGround;
- dColor.addElement(prevColor); // add the color to start line 0
- }
- else
- {
- dColor.addElement(prevColor);
- prevColor = ((c=getEndColor (trimStr)) == null) ? prevColor : c;
- }
- //System.out.println("cnt: " + cnt
- cnt++;
- }
-
- repaint();
- }
-
- // finds the next string delimited by HTML <br>
- public String getNextString()
- { int Start = curPos;
- int End = curPos;
- boolean found = false;
- if (Start > Text.length() || Text.length() == 0) return null;
- while (curPos < Text.length())
- {
- char c = Text.charAt(curPos);
- if (curPos+3 < Text.length() && Text.charAt(curPos) == '<' && (Text.charAt(curPos+1) == 'b' ||
- Text.charAt(curPos+1) == 'B') && (Text.charAt(curPos+2) == 'r' || Text.charAt(curPos+2) == 'R')
- && Text.charAt(curPos+3) == '>')
- { End = curPos;
- curPos += 4;
- found = true;
- break;
- }
- curPos++;
- //c = 'a';
- }
-
- if (curPos == Text.length())
- { if (End == Start)
- { curPos++; // increment so next call to getNextString() will return null
- return Text.substring(Start, curPos-1);
- }
- else return Text.substring(Start, End);
- }
- else if (End == Start)
- { if (found) return (" "); // return blank line (well almost blank with one space). we do this so calling routine reads another line.
- return null;
- }
- else
- {
- return (Text.substring(Start, End));
- }
- }
-
- public void resize (int width, int height)
- { this.width = width;
- this.height = height;
- repaint();
- }
-
- public synchronized void reshape (int left, int top, int width, int height)
- { this.left = left;
- this.top = top;
- this.width = width;
- this.height = height;
- super.reshape(left, top, width, height);
- lines = 0; // reset the number of showing lines in text area
-
- repaint();
- }
-
- public synchronized void setBackground(Color c)
- {
- super.setBackground(c);
- backGround = c;
-
- }
-
- public synchronized void setForeground(Color c)
- {
- super.setForeground(c);
- foreGround = c;
- }
-
- public void verticalScrollbarAction(int Action, Object arg)
- {
- int sPos = ((Integer) arg).intValue();
- topLine = sPos;
- if (topLine != prevTopLine)
- repaint();
- prevTopLine = topLine;
- }
-
- public void horizontalScrollbarAction(int Action, Object arg)
- {
- int sPos = ((Integer) arg).intValue();
- viewLeftPos = -sPos * aveCharWidth;
- repaint();
- }
-
- // get color for end of line by reading FONT tag
- public Color getEndColor (String Str)
- {
- int fPos, cPos, pos = 0;
- String upStr = Str.toUpperCase();
-
- if ((fPos=upStr.lastIndexOf("<FONT")) > -1 && (cPos=upStr.lastIndexOf("COLOR=")) > -1)
- {
- cPos += 6;
- if (Str.charAt(cPos)=='\"') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='\'') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='#')
- { cPos++; // find beginning of hex or long color number
-
- int Start = cPos; // starting position of number
- while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
- String hexString = upStr.substring(Start, cPos); // get number as string
- while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number
- if (hexString.length() != 6) return null;
- int intColor = Integer.parseInt(hexString, 16); // convert from hex string to int
- return (new Color(intColor)); // new color
- }
- else
- return null;
- }
- else
- return null;
- }
-
- // get color for beginning of each line
- public Color getFirstColor (String Str)
- {
- int fPos, cPos, pos = 0;
- String upStr = Str.toUpperCase();
-
- if ((fPos=upStr.indexOf("<FONT", pos)) > -1 && (cPos=upStr.indexOf("COLOR=", pos)) > -1)
- {
- cPos += 6;
- if (Str.charAt(cPos)=='\"') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='\'') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='#')
- { cPos++; // find beginning of hex or long color number
-
- int Start = cPos; // starting position of number
- while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
- String hexString = upStr.substring(Start, cPos); // get number as string
- while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number
- if (hexString.length() != 6) return null;
- int intColor = Integer.parseInt(hexString, 16); // convert from hex string to int
- return (new Color(intColor)); // new color
- }
- else
- return null;
- }
- else
- return null;
-
- }
-
- public void paint(java.awt.Graphics g)
- {
- String Str, resultStr, upStr, drawStr;
-
- Font cFont = new Font("Courier", Font.BOLD, 12);
- g.setFont(cFont);
- //g.setColor(prevTopColor); // get default color
- Color fg = foreGround;
-
- FontMetrics fm = g.getFontMetrics(cFont);
-
- if (aveCharWidth == 0) aveCharWidth = fm.stringWidth("M"); // get the average char width for incremental scrolling
- if (lineHeight == 0) lineHeight = fm.getHeight(); // get the height of a line
-
- if (lines == 0) lines = height/lineHeight;
- int num = ((dText.size() - topLine) < lines) ? dText.size() - topLine : lines;
-
- // cycle through the lines of text and draw to screen
- for (int ii = 0; ii < num; ii++)
- {
-
- if (topLine > dText.size() - 1) break;
- Str = (String)dText.elementAt(ii+topLine);
- g.setColor((Color)dColor.elementAt(ii+topLine));
- //g.setFont((Font)dFont.elementAt(ii+topLine));
- int drawPos = 1; // starting position to draw string
- upStr = Str.toUpperCase();
-
- int cPos = -1;
- int fPos = -1;
- int pos = 0;
- boolean printAsIs = false;
- boolean foundFontTag = false;
- // see if font or color change. If not just draw text for line, else draw in different font and color
- while ((fPos=upStr.indexOf("<FONT", pos)) > -1 && (cPos=upStr.indexOf("COLOR=", pos)) > -1)
- { foundFontTag = true;
- printAsIs = false;
- cPos += 6;
- if (Str.charAt(cPos)=='\"') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='\'') cPos++; // find beginning of hex or long color number
- if (Str.charAt(cPos)=='#') cPos++; // find beginning of hex or long color number
- else
- { printAsIs = true;
- break; // not a valid hex number ("#FF00FF" format)
- }
- int Start = cPos; // starting position of number
- while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
- String hexString = upStr.substring(Start, cPos); // get number as string
- while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number
- if (hexString.length() != 6)
- { printAsIs = true;
- break; // not a valid hex number ("#FF00FF" format)
- }
-
- drawStr = Str.substring(pos, fPos);
- g.drawString(drawStr, drawPos+viewLeftPos, lineHeight * (ii+1)); // draw string in current color
-
- drawPos += fm.stringWidth(drawStr);
- int intColor = Integer.parseInt(hexString, 16); // convert from hex string to int
- fg = new Color(intColor); // set new color
- g.setColor(fg); // return to entry color
- pos = cPos+1;
- }
- if (foundFontTag)
- { g.setColor(fg); // return to entry color
- g.drawString(Str.substring(pos, Str.length()), drawPos+viewLeftPos, lineHeight * (ii+1)); // draw string in current color
- if (first) // find max length of string
- { int wid = drawPos + fm.stringWidth(Str.substring(pos, Str.length())); // add last segment to length
- if (wid > textLengthMax)
- textLengthMax = wid;
- }
-
- }
- if (!foundFontTag || printAsIs)
- { if (first) // find max length of string
- { int wid = fm.stringWidth(Str);
- if (wid > textLengthMax)
- textLengthMax = wid;
- }
- g.drawString (Str, 1+viewLeftPos, lineHeight * (ii+1));
- }
-
- foreGround = fg; // return to entry color
- //if (ii == 0) prevTopColor = fg;
- }
-
- if (first)
- {
-
- textLengthMax = (textLengthMax + 30) / aveCharWidth; // add slop space for scrolling
- if (textCharLenghtMax > textLengthMax) textLengthMax = textCharLenghtMax;
- ViewSourceDialog parent = (ViewSourceDialog) getParent();
- parent.horizontalScrollbar.setPageIncrement(textLengthMax/4);
- parent.horizontalScrollbar.setValues(0, 6, 0, textLengthMax);
-
- totalLines = dText.size();
-
- parent.verticalScrollbar.setPageIncrement(totalLines/4);
- parent.verticalScrollbar.setValues(0, 6, 0, totalLines);
-
- }
- first = false;
- }
-
- public String rTrim(String Str)
- { int iLen, len;
- len = iLen = Str.length() - 1;
- int st = 0;
- while ((len > 0) && (Str.charAt(len) <= ' '))
- {
- len--;
- }
- return (len < iLen) ? Str.substring(st, len+1) : Str;
- }
-
-
- } // End of class textDisplay
-
-
-